﻿using OneToManyEntityFramework.Model;
using OneToManyEntityFramework.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OneToManyEntityFramework.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<Album> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Albums.Include("Songs").Where(x => x.DateDeleted == null).ToList();
            }
            return View(model);
        }

        public ActionResult Details(int id)
        {
            Album model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Albums.Include("Songs").FirstOrDefault(x => x.AlbumId == id);
                //Include function includes the virtual List Songs property in Album.cs
                //Puts it in a list for us and allows us to display it in Details view
            }
            return View(model);
        }

        //id is the AlbumId for the Song
        public ActionResult AddSong(int id)
        {
            ViewBag.Message = "Add A New Song";
            Song model = new Song()
            {
                AlbumId = id
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult AddSong(Song model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Song newSong = new Song()
                {
                    AlbumId = model.AlbumId,
                    DateReleased = model.DateReleased,
                    Title = model.Title
                };
                db.Songs.Add(newSong);
                db.SaveChanges();
            }

            return RedirectToAction("Details", new { id = model.AlbumId });
        }

        //id is the Song Id
        public ActionResult EditSong(int id)
        {
            ViewBag.Message = "Edit Song";
            Song song;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                song = db.Songs.FirstOrDefault(x => x.SongId == id);
            }
            return View("AddSong", song);
        }

        [HttpPost]
        public ActionResult EditSong(Song model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Song song = db.Songs.FirstOrDefault(x => x.SongId == model.SongId);
                song.DateReleased = model.DateReleased;
                song.Title = model.Title;
                db.SaveChanges();
            }
            return RedirectToAction("Details", new { id = model.AlbumId });
        }

        //Id is the Album Id
        public ActionResult DeleteAlbum(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Album model = db.Albums.FirstOrDefault(x => x.AlbumId == id);
                model.DateDeleted = DateTime.Now;
                //db.Albums.Remove(model);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}